import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import useFileInfo from '/@/api/jdfsystem/file-info';
import MESSAGE from '/@/enums/message-enum';
import { BaseRequest, BaseResponse } from '/@/interfaces/BaseModel';
import {
    FileInfoSearchParamModel,
    FileInfoAddParamModel,
    FileInfoDetailModel,
    FileInfoListModel,
} from '/@/interfaces/JdfsystemModel';
import useObjectUtil from '/@/utils/objectUtil';

const handle = useFileInfo();

async function handleSearch(form: BaseRequest<FileInfoSearchParamModel>) {
    const response = await handle.list(form);
    return response.data;
}

async function handleDetail(id: string) {
    const response = await handle.detail({ id: id });
    return response.data;
}

async function handleInfo(ids: Array<string>) {
    const response = await handle.listFileInfo({ param: ids, pageInfo: undefined });
    return response.data;
}

async function handleAdd(form: FileInfoAddParamModel, callback: () => void) {
    const response = await handle.add(form);
    const { code, msg } = response.data;
    if (MESSAGE.OK === code) {
        ElMessage.success(msg);
        callback();
    }
    return response.data;
}

async function handleDel(idList: string[]) {
    const response = await handle.del({ param: idList, pageInfo: undefined });
    const { code, msg } = response.data;
    if (MESSAGE.OK === code) {
        ElMessage.success(msg);
    }
    return response.data;
}

async function handleValidateForm(formRef: FormInstance | undefined) {
    let flag = false;
    if (!formRef) {
        return flag;
    }
    await formRef.validate((valid, fields) => {
        console.debug(fields);
        flag = valid;
    });
    return flag;
}

function handleIsMP4(fileInfo: FileInfoDetailModel): boolean {
    if (!fileInfo || !fileInfo.fileExt) {
        return false;
    }
    return fileInfo.fileExt === 'mp4' || fileInfo.fileExt === 'MP4';
}

function handleBuildUploadInfo(fileIdList: string[] = [], fileInfoList: FileInfoListModel[] = []): { name: string, url: string, response: BaseResponse<string> }[] {
    const imageInfoList: { name: string, url: string, response: BaseResponse<string> }[] = [];
    const map = new Map<String, FileInfoListModel>();
    fileInfoList.forEach(f => map.set(f.id as string, f));
    fileIdList
        .filter(f => useObjectUtil.isNotNull(f))
        .forEach(f => {
            const fileInfo: FileInfoListModel | undefined = map.get(f);
            const fileName: string = fileInfo ? fileInfo.fileName + '' : f;
            imageInfoList.push({ name: fileName, url: handle.view(f, fileInfo?.platformId as string), response: { code: MESSAGE.OK, data: f, msg: '', pageInfo: { page: 1, limit: 0, total: 0 } } });
        });
    return imageInfoList;
}

async function handleTrust(platformId: string, fileId: string, flag: boolean = false) {
    const response = await handle.trust({ platformId: platformId, id: fileId, flag: flag });
    const { code, msg } = response.data;
    if (MESSAGE.OK === code) {
        ElMessage.success(msg);
    }
    return response.data;
}

export {
    handleSearch,
    handleDetail,
    handleAdd,
    handleDel,
    handleValidateForm,
    handleIsMP4,
    handleBuildUploadInfo,
    handleTrust,
    handleInfo,
}
